home *** CD-ROM | disk | FTP | other *** search
/ NeXT Education Software Sampler 1992 Fall / NeXT Education Software Sampler 1992 Fall.iso / Programming / Source / 2DLab / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-14  |  1.2 KB  |  65 lines

  1. #include "stdio.h"
  2. #include "voronoi.h"
  3.  
  4. freeinit(fl, size)
  5. struct    Freelist *fl;
  6. int    size;
  7. {
  8.     fl->head = (struct Freenode *) NULL;
  9.     fl->nodesize = size;
  10. }
  11.  
  12. char *getfree(fl)
  13. struct    Freelist *fl;
  14. {
  15.     int i; struct Freenode *t;
  16.  
  17.     if(fl->head == (struct Freenode *) NULL) {    
  18.     t = (struct Freenode *)myalloc(sqrt_nsites * fl->nodesize);
  19.     for(i=0; i<sqrt_nsites; i+=1)     
  20.         makefree((struct Freenode *)((char *)t+i*fl->nodesize), fl);
  21.     }
  22.     t = fl->head;
  23.     fl->head = (fl->head)->nextfree;
  24.     return((char *)t);
  25. }
  26.  
  27. makefree(curr,fl)
  28. struct Freenode *curr;
  29. struct Freelist *fl;
  30. {
  31.     curr->nextfree = fl->head;
  32.     fl->head = curr;
  33. }
  34.  
  35. #define MAXALLOCS (8 * MAXSITES)
  36. int total_alloc, num_allocs;
  37. char *locations[MAXALLOCS];
  38.  
  39. char *myalloc(n)
  40. unsigned n;
  41. {
  42.     char *t;
  43.     if ((t=malloc(n)) == (char *) 0) {
  44.     fprintf(stderr,"out of memory processing site %d (%d bytes in use)\n",
  45.         siteidx, total_alloc);
  46.         exit();
  47.     }
  48.     total_alloc += n;
  49.     locations[num_allocs++] = t;
  50.     return(t);
  51. }
  52.  
  53. void 
  54. myfreeall()
  55. {
  56. int k;
  57.  
  58.     for (k = 0; k < num_allocs; k++)
  59.     free (locations [k]);
  60. /*
  61.     printf ("freed %d allocs (%d bytes)\n", num_allocs, total_alloc);
  62. */
  63.     num_allocs = total_alloc = 0;
  64. }
  65.